package com.example.easyutils.dram;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.InputDel;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class DramActivity extends BaseActivity {
    private EditText editText;
    private LinearLayoutManager mLinearLayoutManager;
    private RecyclerView recyclerView;
    List mList;
    private static final int SUCCESS_CODE = 1,ERROR_CODE = 2;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUCCESS_CODE:
                    Log.d("mList",mList+"");
                    if(mList.size()==0){
                        Toast.makeText(DramActivity.this,"没有数据",Toast.LENGTH_LONG).show();
                        return;
                    }
                    DramRecycleView dramRecycleView = new DramRecycleView(DramActivity.this,mList);
                    mLinearLayoutManager = new LinearLayoutManager(DramActivity.this,RecyclerView.VERTICAL,false);
                    recyclerView.setLayoutManager(mLinearLayoutManager);
                    recyclerView.setAdapter(dramRecycleView);

                    break;
                case ERROR_CODE:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dram_layout);
        editText = (EditText)findViewById(R.id.editText);
        recyclerView = (RecyclerView)findViewById(R.id.dramRecycleView);
        ImageView imageView = null;
        InputDel inputDel = new InputDel(editText,imageView,DramActivity.this);
        inputDel.initListener();
        inputDel.editDel();
    }

    public void keyWordQuery(View v){
        String str = editText.getText().toString();
//           http://api.avatardata.cn/ZhouGongJieMeng/LookUp?key=1bda7de4a13f4ab5a25eb731cac56c1b&keyword=梦见妈妈
        HttpGetRequest.sendOkHttpGetRequst("http://api.avatardata.cn/ZhouGongJieMeng/LookUp?key=1bda7de4a13f4ab5a25eb731cac56c1b&keyword="+str,
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(ERROR_CODE);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Log.d("SUCCESS_CODE",result);
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, Object>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,Object> dataDetail = gson.fromJson(result,new TypeToken<Map<String,Object>>(){

                        }.getType());
                        mList = new ArrayList();
                        mList = (List) dataDetail.get("result");
                        handler.sendEmptyMessage(SUCCESS_CODE);

                    }
                });
    }
}
